package api

import (
	"FeedXmh/app/dao"
	jwt "github.com/gogf/gf-jwt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/util/gconv"
)

var Room = room{}
type room struct {}

func (a *room)GET(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
		"data": nil,
		"count":0,
	}
	params := r.GetQueryMap()


	id :=gconv.String(params["id"])
	houseid :=gconv.String(params["houseid"])

	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path

	db:=g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url,r.Method, userid) {
		model := db.Model("t_room")
		if id != "" {
			model.Where(" id=? ",id)
		}
		if houseid != "" {
			model.Where(" houseid=? ",houseid)
		}

		r, err := model.All()
		if err != nil {
			g.Log().Line().Error(err)
			res["code"]=-1
			res["msg"]=err.Error()
		}else{
			res["data"] = r.List()
			res["count"] = len(r.List())
		}
	}else{
		res["code"]=-1
		res["msg"]="您没有该操作权限"
	}

	r.Response.WriteJson(res)
	r.ExitAll()
}


func (a *room)PUT(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
		"data": nil,
		"count":0,
	}
	params := r.GetFormMap()



	claims := jwt.ExtractClaims(r)
	current_user := claims["userid"].(string)
	url := r.URL.Path

	params["lpt"] = gtime.Now().String()
	params["whom"] = current_user
	//db:=g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url,r.Method, current_user) {
		_,err:=dao.T_room.Save(params)
		if err!=nil{
			g.Log().Line().Error(err)
			res["code"]=-1
			res["msg"]=err.Error()
		}
	}else{
		res["code"]=-1
		res["msg"]="您没有该操作权限"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *room)POST(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
		"data": nil,
		"count":0,
	}
	params := r.GetFormMap()

	_,exist:=params["id"]
	if exist{
		delete(params,"id")
	}

	claims := jwt.ExtractClaims(r)
	current_user := claims["userid"].(string)
	url := r.URL.Path
	params["lpt"] = gtime.Now().String()
	params["whom"] = current_user
	//db:=g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url,r.Method, current_user) {
		r,err:=dao.T_room.Save(params)
		if err!=nil{
			g.Log().Line().Error(err)
			res["code"]=-1
			res["msg"]=err.Error()
		}else{
			_id,_:= r.LastInsertId()
			res["code"]=0
			res["data"] = _id
		}
	}else{
		res["code"]=-1
		res["msg"]="您没有该操作权限"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *room)DELETE(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
		"data": nil,
		"count":0,
	}
	params := r.GetFormMap()

	id,exist:=params["id"]
	if exist{
		claims := jwt.ExtractClaims(r)
		current_user := claims["userid"].(string)
		url := r.URL.Path
		params["lpt"] = gtime.Now().String()
		params["whom"] = current_user
		//db:=g.DB(g.Cfg().GetString("custom.db_name"))
		if dao.CheckApiPermission(url,r.Method, current_user) {
			_,err:=dao.T_room.Delete(gconv.String(id))
			if err!=nil{
				g.Log().Line().Error(err)
				res["code"]=-1
				res["msg"]=err.Error()
			}
		}else{
			res["code"]=-1
			res["msg"]="您没有该操作权限"
		}
	}else{
		res["code"]=-1
		res["msg"]="错误的参数"
	}


	r.Response.WriteJson(res)
	r.ExitAll()
}